package com.wenming.library;

import ohos.agp.components.ComponentProvider;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.event.intentagent.IntentAgent;
import ohos.event.notification.*;
import ohos.media.image.PixelMap;
import ohos.rpc.RemoteException;

import java.util.ArrayList;
import java.util.Calendar;

public class NotifyUtil {

    private final NotificationSlot mSlot;

    public NotifyUtil() {
        mSlot = getNotificationSlot("1", true);
    }

    /**
     * 高仿淘宝
     *
     * @param title          标题
     * @param conStr         内容
     * @param littleIcon     小图标
     * @param intentAgent    intent
     * @param notificationId id
     */
    public void notify_normal_singLine(String title, String conStr, PixelMap littleIcon,
                                       IntentAgent intentAgent, int notificationId) {
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);

        // 普通文本
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title)
                .setText(conStr);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        // 设置通知的内容
        request.setContent(notificationContent);
        request.setIntentAgent(intentAgent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 高仿网易新闻
     *
     * @param title          标题
     * @param longText       长文本
     * @param littleIcon     小图标
     * @param intentAgent    intent
     * @param notificationId id
     */
    public void notify_normal_moreLine(String title, String longText, PixelMap littleIcon,
                                       IntentAgent intentAgent, int notificationId) {
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);

        // 长文本
        NotificationRequest.NotificationLongTextContent contentLong = new NotificationRequest.NotificationLongTextContent();
        contentLong.setTitle(title)
                .setLongText(longText);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(contentLong);
        // 设置通知的内容
        request.setContent(notificationContent);
        request.setIntentAgent(intentAgent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 收件箱样式
     *
     * @param name           发件人
     * @param arrayList      信息
     * @param littleIcon     小图标
     * @param icon           头像
     * @param intentAgent    intent
     * @param notificationId id
     */
    public void notify_mailbox(String name, ArrayList<String> arrayList, PixelMap littleIcon, PixelMap icon,
                               IntentAgent intentAgent, int notificationId) {
        MessageUser messageUser = new MessageUser();
        messageUser.setName(name);
        messageUser.setPixelMap(icon);

        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);
        request.addMessageUser(messageUser);

        // 社交
        NotificationRequest.NotificationConversationalContent content = new NotificationRequest.NotificationConversationalContent(messageUser);
        content.setConversationTitle("[" + arrayList.size() + "条]" + name)
                .setConversationGroup(true);
        for (int i = 0; i < arrayList.size(); i++) {
            content.addConversationalMessage(arrayList.get(i), 1, messageUser);
        }

        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        // 设置通知的内容
        request.setContent(notificationContent);
        request.setIntentAgent(intentAgent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 高仿系统截图通知
     *
     * @param title          标题
     * @param context        context
     * @param littleIcon     小图标
     * @param icon           大图标
     * @param intentAgent    intent
     * @param notificationId id
     */
    public void notify_bigPic(String title, String context, PixelMap littleIcon, PixelMap icon,
                              IntentAgent intentAgent, int notificationId) {
        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);
        request.setBigIcon(icon);

        // 长文本
        NotificationRequest.NotificationPictureContent contentLong = new NotificationRequest.NotificationPictureContent();
        contentLong.setTitle(title)
                .setBigPicture(icon)
                .setExpandedTitle(title)
                .setText(context);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(contentLong);
        // 设置通知的内容
        request.setContent(notificationContent);
        request.setIntentAgent(intentAgent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 自定义视图的通知
     *
     * @param context        上下文
     * @param layoutId       自定义布局
     * @param viewId         按钮id
     * @param littleIcon     图标
     * @param intentAgent    intent
     * @param intentAgentNo  intent
     * @param notificationId id
     */
    public void notify_customview(Context context, int layoutId, int viewId, PixelMap littleIcon,
                                  IntentAgent intentAgent, IntentAgent intentAgentNo, int notificationId) {
        NotificationRequest request = new NotificationRequest(context, notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);

        String title = "";
        String text = "";
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title).setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);

        ComponentProvider componentProvider = new ComponentProvider(layoutId, context); // 创建ComponentProvider对象
//        componentProvider.setString(ResourceTable.Id_ongoing_card_text, "setText", "TextContent"); // 设置布局中的文本内容
        componentProvider.setIntentAgent(viewId, intentAgentNo);
        request.setCustomView(componentProvider);
        request.setIntentAgent(intentAgent);

        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 里面有两个按钮的通知
     *
     * @param name           标题
     * @param text           内容
     * @param btn1           按钮
     * @param btn2           按钮
     * @param littleIcon     图标
     * @param intentAgent    目标
     * @param intentAgentNo  目标
     * @param notificationId id
     */
    public void notify_button(String name, String text, String btn1, String btn2,
                              PixelMap littleIcon, IntentAgent intentAgent,
                              IntentAgent intentAgentNo, int notificationId) {
        NotificationActionButton.Builder builder = new NotificationActionButton.Builder(null, btn1, null);
        NotificationActionButton.Builder builder1 = new NotificationActionButton.Builder(null, btn2, intentAgentNo);

        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);
        request.addActionButton(builder.build());
        request.addActionButton(builder1.build());

        // 普通文本
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(name)
                .setText(text);

        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        // 设置通知的内容
        request.setContent(notificationContent);
        request.setIntentAgent(intentAgent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 高仿Ohos系统下载样式
     *
     * @param context        上下文
     * @param title          标题
     * @param text1          文案
     * @param text2          文案
     * @param littleIcon     图标
     * @param intentAgent    目标
     * @param notificationId id
     */
    public void notify_progress(Context context, String title, String text1, String text2, PixelMap littleIcon,
                                IntentAgent intentAgent, int notificationId) {
        NotificationRequest request = new NotificationRequest(context, notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);

        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title).setText(text1);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);


        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i+=10) {
                    request.setProgressBar(i, 100, false);
                    request.setIntentAgent(intentAgent);

                    try {
                        NotificationHelper.publishNotification(request);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }

                    try {
                        Thread.sleep(1 * 500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                content.setTitle(title).setText(text2);
                NotificationRequest.NotificationContent notificationContent1 = new NotificationRequest.NotificationContent(content);
                request.setContent(notificationContent1);
                request.setProgressBar(0, 0, false);
                request.setIntentAgent(intentAgent);

                try {
                    NotificationHelper.publishNotification(request);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 悬浮式通知
     *
     * @param name           发件人
     * @param text           信息
     * @param btn1           按钮
     * @param btn2           按钮
     * @param littleIcon     小图标
     * @param icon           头像
     * @param intentAgent    intent
     * @param intentAgentNo  intent
     * @param notificationId id
     */
    public void notify_headUp(String name, String text, String btn1, String btn2,
                              PixelMap littleIcon, PixelMap icon, IntentAgent intentAgent,
                              IntentAgent intentAgentNo, int notificationId) {
        NotificationActionButton.Builder builder = new NotificationActionButton.Builder(null, btn1, null);
        NotificationActionButton.Builder builder1 = new NotificationActionButton.Builder(null, btn2, intentAgentNo);

        NotificationRequest request = new NotificationRequest(notificationId);
        request.setSlotId(mSlot.getId());
        request.setLittleIcon(littleIcon);
        request.addActionButton(builder.build());
        request.addActionButton(builder1.build());
        request.setBigIcon(icon);

        // 普通文本
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(name)
                .setText(text);

        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        // 设置通知的内容
        request.setContent(notificationContent);
        request.setIntentAgent(intentAgent);
        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 配置NotificationSlot
     *
     * @param id       id
     * @param isRemind 是否振动
     * @return NotificationSlot
     */
    private NotificationSlot getNotificationSlot(String id, boolean isRemind) {
        // 创建notificationSlot对象
        NotificationSlot slot = new NotificationSlot(id, "其他", NotificationSlot.LEVEL_HIGH);
        slot.setDescription("notify_normal_singLine");
        slot.setLevel(NotificationSlot.LEVEL_HIGH);
        // 设置振动提醒
        slot.setEnableVibration(isRemind);
        // 设置锁屏模式
        slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PUBLIC);
        // 设置开启呼吸灯提醒
        slot.setEnableLight(isRemind);
        // 设置呼吸灯的提醒颜色
        slot.setLedLightColor(Color.RED.getValue());
        slot.enableBypassDnd(true);
        slot.enableBadge(true);
        try {
            NotificationHelper.addNotificationSlot(slot);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return slot;
    }

    private String getCurrentTime() {
        String timeStr = "";
        long time = System.currentTimeMillis();
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int apm = calendar.get(Calendar.AM_PM);
        if (apm == 0) {
            timeStr = "上午";
        } else if (apm == 1) {
            timeStr = "下午";
        }
        int hour = calendar.get(Calendar.HOUR);
        int minuts = calendar.get(Calendar.MINUTE);
        return timeStr + " " + hour + ":" + minuts;
    }
}
